package com.lysh.http.handler;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author liyishan
 * @date 2023/4/19 13:09
 */

public class DefaultHttpClient extends CloseableHttpClient {
    private static final Logger log = LoggerFactory.getLogger(DefaultHttpClient.class);

    private static CloseableHttpClient closeableHttpClient;

    public DefaultHttpClient() {
        this.init();
    }

    @Override
    protected CloseableHttpResponse doExecute(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext) throws IOException, ClientProtocolException {
        return closeableHttpClient.execute(httpHost, httpRequest, httpContext);
    }


    @Override
    public void close() throws IOException {
        log.info("销毁DefaultHttpClient");
        closeableHttpClient.close();
    }

    private void init() {

        log.info("初始化DefaultHttpClient");
    }

    static {
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(40000).setSocketTimeout(30000).build();
        closeableHttpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setMaxConnTotal(300).setMaxConnPerRoute(150).build();
    }


    @Override
    public HttpParams getParams() {
        return null;
    }

    @Override
    public ClientConnectionManager getConnectionManager() {
        return null;
    }
}
